package handlers

import (
	"MBTIx/dba"
	"MBTIx/models"
	"net/http"

	"github.com/gin-gonic/gin"
	"github.com/jmoiron/sqlx"
)

func Post_test(c *gin.Context) {
	db, _ := c.Get("db") //读取数据库连接池
	var test models.Test
	var student models.Student
	c.ShouldBind(&test)
	dba.GetStudent(db.(*sqlx.DB), test.User_id, &student)
	student.Result = test.Answer
	dba.UpdateStudent(db.(*sqlx.DB), student)
	dba.InsertTest(db.(*sqlx.DB), test) //插入数据

	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "seccess",
	})
}

func Get_Test(c *gin.Context) {
	db, _ := c.Get("db")

	var tests []models.Test
	dba.GetTest(db.(*sqlx.DB), &tests)

	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"data": gin.H{
			"tests": tests,
		},
	})
}
